﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Games;
using Players;
using Rooms;

namespace Channels
{
    public class Channel
    {
        int iChannelID;

        public int IChannelID
        {
            get { return iChannelID; }
            set { iChannelID = value; }
        }

        string strChannelName;

        public string StrChannelName
        {
            get { return strChannelName; }
            set { strChannelName = value; }
        }

        int iMaxPlayer=100;

        public int IMaxPlayer
        {
            get { return iMaxPlayer; }
            set { iMaxPlayer = value; }
        }

        int iJoinedPlayer=0;

        public int IJoinedPlayer
        {
            get { return iJoinedPlayer; }
            set { iJoinedPlayer = value; }
        }

        static List<Channel> lstChannels;

        public static List<Channel> LstChannels
        {
            get { return lstChannels; }
            set { lstChannels = value; }
        }

        List<Game> lstGames;

        private List<Game> LstGames
        {
            get { return lstGames; }
            set { lstGames = value; }
        }

        static List<Player> lstPlayer;

        public static List<Player> LstPlayer
        {
            get { return Channel.lstPlayer; }
            set { Channel.lstPlayer = value; }
        }

        List<Room> lstRoom;

        public List<Room> LstRoom
        {
            get { return lstRoom; }
            set { lstRoom = value; }
        }

        
        public Channel()
        {
            
        }

        public Channel(int iChnID, string strChnName)
        {
            IChannelID = iChnID;
            StrChannelName = strChnName;
            LstRoom = new List<Room>(10);
            GameTypes objGT = new GameTypes();
            objGT = GameTypes.BaCay;
            for (int i = 1; i <= 10; i++)
            {
                LstRoom.Add(new Room(new Game(i, objGT), i, i, "Room " + i.ToString(), 4, 1000, null));
            }
            /*
            GameTypes objGT = new GameTypes();
            objGT = GameTypes.BaCay;
            lstGames = new List<Game>(10);
            for (int i = 1; i <= 10; i++)
                LstGames.Add(new Game(i, objGT));*/
        }

        public static bool joinChannel(int iChnID, Player ObjPlayer)
        {
            Channel selectedChannel = LstChannels.Find(x => x.IChannelID == iChnID);
            if (selectedChannel.IJoinedPlayer < selectedChannel.IMaxPlayer)
            {
                //Player Objtemp = LstPlayer.Find(x => x.StrAccount == ObjPlayer.StrAccount);
                if (LstPlayer == null)
                    LstPlayer = new List<Player>();
                LstPlayer.Add(ObjPlayer);
                selectedChannel.IJoinedPlayer += 1;
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool outChannel(int iChnID, Player ObjPlayer)
        {
            Channel selectedChannel = LstChannels.Find(x => x.IChannelID == iChnID);
            if (selectedChannel.IJoinedPlayer < selectedChannel.IMaxPlayer)
            {
                Player Objtemp = LstPlayer.Find(x => x.StrAccount == ObjPlayer.StrAccount);
                if (LstPlayer.Remove(ObjPlayer))
                {
                    selectedChannel.IJoinedPlayer -= 1;
                    return true;
                }
                else
                    return false;
            }
            else
            {
                return false;
            }
        }
    }
}
